package com.supermap.business.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author aqua
 * @desc
 * @date 2019年05月30 16:32
 * @email 1301584033@qq.com
 */
public class TakeWife {

        public static void main(String[] args) {
            List<Challenger> isEnd = new ArrayList<Challenger>();
            for(int i=0;i<100;i++){
                TakeWife takeWife = new TakeWife();
                isEnd.add(takeWife.start());
            }
            int a=0;
            int b=0;
            int c=0;
            for(Challenger challenger:isEnd){
                if(challenger.getName().equals("one")){
                    a++;
                }
                if(challenger.getName().equals("two")){
                    b++;
                }
                if(challenger.getName().equals("three")){
                    c++;
                }
            }
            System.out.println(a+"-------"+b+"-----"+c);

        }

        public Challenger  start() {
            Challenger one = new Challenger("one", 0.30);
            Challenger two = new Challenger("two", 0.50);
            Challenger three = new Challenger("three", 1.00);
            List<Challenger> isEnd = new ArrayList<Challenger>();
            isEnd.add(one);
            System.out.println("选手" + one + "入场");
            isEnd.add(two);
            System.out.println("选手" + two + "入场");
            isEnd.add(three);
            System.out.println("选手" + three + "入场");
            System.out.println("挑战者全部就位");
            // 为了女人他们开始了拼杀 -<-<--
            while (isEnd.size() != 1) {
                happen(isEnd);
            }
            System.out.println("胜利者"+isEnd.toString());
           return  isEnd.get(0);
        }

        public void happen(List<Challenger> isEnd) {
            // 根据命中率重新排序 -<-<--
            for (int i = 0; i < isEnd.size() - 1; i++) {
                if (isEnd.get(i).getHit_rate() > isEnd.get(i + 1).getHit_rate()) {
                    Challenger turn;
                    turn = isEnd.get(i);
                    isEnd.set(i, isEnd.get(i + 1));
                    isEnd.set(i + 1, turn);
                }
            }
            // 一轮枪击活动 -<-<--
            int shou = isEnd.size() - 1;
            for (int x = 0; x < isEnd.size(); x++) {
                int gong = x;
                if (gong == shou) {
                    shou -= 1;
                }
                System.out.print(isEnd.get(gong).getName() + "向"
                        + isEnd.get(shou).getName() + "发起了攻击:>>>>");
                isEnd.get(gong).setNum(isEnd.get(gong).getNum() + 1);
                if (attack(isEnd.get(gong).getHit_rate())) {
                    isEnd.get(shou).setAlive(false);
                    System.out.println(isEnd.get(shou).getName() + "被"
                            + isEnd.get(gong).getName() + "击杀了");
                    isEnd.remove(isEnd.get(shou));
                    shou -= 1;
                } else {
                    System.out.println("----未击中");
                }
            }
        }

        // 命中率操作 -<-<--
        public boolean attack(Double rate) {
            boolean flag = true;
            int i = (int) (rate * 100);
            Random random = new Random();
            int j = random.nextInt(100);
            if (j >= i) {
                flag = false;
            }
            return flag;
        }

}
